import url from '../data/url'
import request from '../data/request'
import { header } from '../data/config'
import store from 'store'

const base                  = url.base,
      onlyUser              = request.onlyUser,
      checkPropertyName     = request.checkPropertyName,
      checkPropertyTypeName = request.checkPropertyTypeName,
      checkDeveloperName    = request.checkDeveloperName,
      checkKeySnName        = request.checkKeySnName,
      checkBuildName        = request.checkBuildName,
      checkTerminalId       = request.checkTerminalId,
      checkAdminNo          = request.checkAdminNo;

// 自定义验证

// 不能有空白字符
$.validator.addMethod("noSpace", function (value, element) {
  return this.optional(element) || (!/\s/g.test(value))
}, "不能包含空白字符")

// 两端不能有空白字符
$.validator.addMethod("noSideSpace", function (value, element) {
  return this.optional(element) || (!/(^\s+)|(\s+$)/g.test(value))
}, "两端不能包含空白字符")

// 左边不能有空白字符
$.validator.addMethod("noLeftSpace", function (value, element) {
  return this.optional(element) || (!/^\s+/g.test(value))
}, "开始不能包含空白字符")

// 右边不能有空白字符
$.validator.addMethod("noRightSpace", function (value, element) {
  return this.optional(element) || (!/\s+$/g.test(value))
}, "结尾不能包含空白字符")

// 用户
$.validator.addMethod("user", function (value, element) {
  return this.optional(element) || (/^[a-zA-Z0-9]+$/i.test(value))
}, "只能输入英文或者数字")

// number or character
$.validator.addMethod("numberOrCharacter", function (value, element) {
  return this.optional(element) || (/^[a-zA-Z0-9]+$/i.test(value))
}, "只能输入英文或者数字")

// 汉字
$.validator.addMethod("hanzi", function (value, element) {
  return this.optional(element) || (/^[\u4e00-\u9fa5]{0,}$/g.test(value))
}, "只能输入汉字")

// 手机号码
$.validator.addMethod("phone", function (value, element) {
  // return this.optional(element) || (/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(value))
  return this.optional(element) || (/^1\d{10}$/.test(value))
}, "输入的手机号码格式错误")

// 多个手机号码
$.validator.addMethod("morePhone", function (value, element) {
  return this.optional(element) || (/^1\d{10}((,|;|\s)1\d{10})*$/.test(value))
}, "输入的手机号码格式错误")

// url
$.validator.addMethod("url", function (value, element) {
  return this.optional(element) || (/^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i.test(value))
}, "输入的url地址不合法")

// 身份证号码
$.validator.addMethod("idcard", function (value, element) {
  return this.optional(element) || (/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(value))
}, "输入的身份证号码格式错误")

// 密码
$.validator.addMethod("pwd", function (value, element) {
  return this.optional(element) || (/^(?![0-9]+$)(?![a-zA-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-zA-Z]|[0-9]){1,}$/.test(value))
}, "必须为数字、符号、字母任意两种以上字符组成")

// 是否有上传图片
$.validator.addMethod("checkImg", function (value, element) {
  let img = $('.uploadImg').attr('src');
  return img !== '';
}, "上传图片不能为空")

// 房号
$.validator.addMethod("room", function (value, element) {
  return this.optional(element) || (/^[1-9]\d*\-[1-9]\d*\-[1-9]\d*$|^[1-9]\d*\-[1-9]\d*$/.test(value))
}, "格式为\"数字-数字\",最多三组")

// 检查用户名重复
$.validator.addMethod("checkUserExist", function (value, element) {
  return checkUserExist(value)
}, "用户名已存在")

// 检查手机号码重复
$.validator.addMethod("checkPhoneExist", function (value, element) {
  return checkPhoneExist(value)
}, "手机号码重复注册")

// 检查楼盘名是否重复
$.validator.addMethod("checkPropertyName", function (value, element) {
  element.parents()
  return checkUserExist(value)
}, "用户名已存在")

// 检查用户名重复
$.validator.addMethod("checkUserExist", function (value, element) {
  return checkUserExist(value)
}, "用户名已存在")

// 检查楼盘重复
$.validator.addMethod("checkProperty", function (value, element) {
  return checkProperty(value)
}, "该楼盘已存在")

// 检查楼盘户型重复
$.validator.addMethod("checkPropertyType", function (value, element) {
  return checkPropertyType(value)
}, "该户型已存在")

// 检查开发商重复
$.validator.addMethod("checkDeveloper", function (value, element) {
  return checkDeveloper(value)
}, "该开发商已存在")

// 检查终端号重复
$.validator.addMethod("checkTerminal", function (value, element) {
  return checkTerminal(value)
}, "该终端号已存在")

// 检查键盘设备码重复
$.validator.addMethod("checkKeySnName", function (value, element) {
  return checkKeySn(value)
}, "该键盘设备码已存在")

// 检查对内楼盘名称重复
$.validator.addMethod("checkBuildInName", function (value, element) {
  return checkBuildIn(value)
}, "该对内楼盘名已存在")

// 检查对外楼盘名称重复
$.validator.addMethod("checkBuildOutName", function (value, element) {
  return checkBuildOut(value)
}, "该对外楼盘名已存在")

// 检查管理者账号的重复
$.validator.addMethod("checkManagerNo", function (value, element) {
  return checkManagerNo(value)
}, "该账号已存在")

// 检查用户名重复
function checkUserExist (value) {

  let result = false

  $.ajax({
    url: base + onlyUser,
    headers: header,
    async: false,
    data: {
      loginName: value
    },
    success: function (res) {
      if (res.code === 0) {
        result = true
      }
    }
  })

  return result

}

// 检查楼盘重复
function checkProperty (value) {

  let result = false

  $.ajax({
    url: base + checkPropertyName,
    async: false,
    headers: header,
    type: 'POST',
    data: {
      propertyName: value,
      enterpriseId: $('#dId').val()
    },
    success: function (res) {
      if (res.code === 0) {
        result = true
      }

    },
    error: function (res) {
    }
  });

  return result

}

// 检查楼盘户型重复
function checkPropertyType (value) {

  let result = false

  $.ajax({
    url: base + checkPropertyTypeName,
    async: false,
    headers: header,
    type: 'POST',
    data: {
      name: value,
      buildingId: $('#buildingId').val(),
    },
    success: function (res) {
      if (res.code === 0) {
        result = true
      }

    },
    error: function (res) {
    }
  });

  return result

}

// 检查终端号重复
function checkTerminal (value) {
  let result = false

  $.ajax({
    url: base + checkTerminalId,
    async: false,
    headers: header,
    type: 'POST',
    data: {
      identifier: value,
    },
    success: function (res) {
      if (res.code === 0) {
        result = true
      }

    },
    error: function (res) {
    }
  });

  return result

}

// 检查终端号重复
function checkDeveloper (value) {

  let result    = false,
      storeData = store.get('storeData'),
      id        = storeData.id;
  console.log(id)
  $.ajax({
    url: base + checkDeveloperName,
    async: false,
    headers: header,
    type: 'POST',
    data: {
      enterpriseName: value,
      id: id,
    },
    success: function (res) {
      if (res.code === 0) {
        result = true
      }

    },
    error: function (res) {
    }
  });

  return result

}

function checkKeySn (val) {
  let result    = false,
      storeData = store.get('storeData');

  if (storeData !== '' && storeData.keySn === val) {
    return true
  }

  $.ajax({
    url: base + checkKeySnName,
    async: false,
    headers: header,
    type: 'POST',
    data: {
      keySn: val
    },
    success: function (res) {
      result = res
    },
    error: function (res) {
    }
  });

  return result
}

function checkBuildIn (val) {
  let result    = false,
      storeData = store.get('storeData');

  if (storeData !== '' && storeData.buildingInfo.propertyName === val) {
    return true
  }

  $.ajax({
    url: base + checkBuildName,
    async: false,
    headers: header,
    type: 'POST',
    data: {
      propertyName: val
    },
    success: function (res) {
      result = res
    },
    error: function (res) {
    }
  });

  return result
}

function checkBuildOut (val) {
  let result    = false,
      storeData = store.get('storeData');

  if (storeData !== '' && storeData.buildingInfo.reserveName === val) {
    return true
  }

  $.ajax({
    url: base + checkBuildName,
    async: false,
    headers: header,
    type: 'POST',
    data: {
      reserveName: val
    },
    success: function (res) {
      result = res
    },
    error: function (res) {
    }
  });

  return result
}

// 检查管理者账号重复
function checkManagerNo (value) {
  if (!value) {
    return true
  }

  let result = false

  $.ajax({
    url: base + checkAdminNo,
    async: false,
    headers: header,
    type: 'POST',
    data: {
      adminNo: value,
    },
    success: function (res) {
      result = res
    },
    error: function (res) {
    }
  });

  return result
}